package com.briup.labs.controller;

import com.briup.labs.pojo.PageBean;
import com.briup.labs.pojo.Result;
import com.briup.labs.pojo.SysRole;
import com.briup.labs.service.SysRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/role")
public class RoleController {

    @Autowired
    private SysRoleService sysRoleService;

    @GetMapping("/all")
    public Result<List<SysRole>> all() {
        List<SysRole> ls = sysRoleService.all();
        return Result.success(ls);
    }

    @GetMapping("/by/condition")
    public Result<PageBean<SysRole>> list(
            String pageNum,
            String pageSize,
            @RequestParam(required = false) String key
    ) {
        PageBean<SysRole> pageBean = sysRoleService.list(pageNum, pageSize, key);
        if (pageBean.getTotal() != null && pageBean.getItems() != null) {
            return Result.success(pageBean);
        } else {
            return Result.error("输出传入格式错误");
        }
    }

    @PostMapping("/saveOrUpdate")
    public Result saveOrUpdate(@RequestBody SysRole sysRole) {
        sysRoleService.saveOrUpdate(sysRole);
        return Result.success();
    }

    @DeleteMapping("/{id}")
    public Result delete(@PathVariable("id") Integer id) {
        sysRoleService.delete(id);
        return Result.success();
    }
}
